package com.xbfinance.portal.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xbfinance.common.util.PropKit;
import com.xbfinance.portal.pojo.po.BannerPic;
import com.xbfinance.portal.pojo.po.P2pProject;
import com.xbfinance.portal.pojo.po.P2pUser;
import com.xbfinance.portal.service.BannerService;
import com.xbfinance.portal.service.ProjectService;
import com.xbfinance.portal.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController {
    @Autowired
    SearchService searchService;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private ProjectService projectService;

    @RequestMapping("/search")
    public String search(@RequestParam(defaultValue = "10") Integer pageSize,
                         @RequestParam(defaultValue = "1") Integer currentPage,
                         String proname, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<P2pProject> pageInfo = searchService.searchProject(proname, currentPage, pageSize);
        System.out.println(pageInfo);
        session.setAttribute("pageInfo", pageInfo);
        return "index";
    }

    @RequestMapping("/searchweb")
    @ResponseBody
    public Map<String, Object> searchWeb(@RequestParam(defaultValue = "10") Integer limit,
                                         @RequestParam(defaultValue = "1") Integer page,
                                         String proname) {
        Map<String, Object> map = new HashMap<>();
        map = searchService.searchProjectOnWeb(proname, page, limit);
        return map;
    }

    @RequestMapping("/searchByName")
    public String searchByName(Model model, @RequestParam(defaultValue = "3") Integer pageSize,
                               @RequestParam(defaultValue = "1") Integer currentPage, String name, HttpSession session) {

        Integer cid = PropKit.use("index.properties").getInt("lunbo.cid");

        List<BannerPic> bannerList = bannerService.queryBanner(cid);

        session.setAttribute("bannerList", bannerList);

        Page<Object> page = PageHelper.startPage(currentPage, pageSize);

        List<P2pProject> list = projectService.queryProjectByName(name);

        PageInfo<P2pProject> pageInfo = new PageInfo<>(list, 3);

        int[] nums = pageInfo.getNavigatepageNums();
        model.addAttribute("nums", nums);
        model.addAttribute("projectList", list);
        model.addAttribute("page", pageInfo);
        model.addAttribute("searchName", name);
        return "index";
    }

    @RequestMapping("/queryProjectByPid")
    public String queryProjectByPid(Long proid, HttpSession session) {

        P2pUser user = (P2pUser) session.getAttribute("user");

        if (user != null) {

            P2pProject project = projectService.queryProjectByPid(proid);

            session.setAttribute("list", project);
            session.setAttribute("user", user);

            return "productinfo";
        }

        return "index";

    }

}
